import { observable, action,toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';
import * as Bctt from "../constants/BaseListConstant";
import { Button } from "antd";

const {TableStore} = WeaTableNew;
const onlySessionKey = Bctt.ONLY_SESSION_KEY;

class BaseListStore {
    @observable loading = false;
    @observable viewTop = true;
    @observable titleName = "";
    @observable titleIcon = "";
    @observable rightMenu = [];
    @observable showSearchAd = false;
    @observable searchKey = "";
    @observable searchConditionForm = {};
    @observable searchConditionComponents = {};
    @observable tableStore = {};
    @observable routerParams = {};
    @observable searchInputName = Bctt.TAB_SEARCH_INPUT_NAME;

    getTableData(requestParams,callBackFn){

    }

    reLoad = () => {
        this.tableStore.getDatas(this.tableStore.dataKey, this.tableStore.current, this.tableStore.pageSize, this.tableStore.sortParams);
    }

    @action
    componentWillMount=(componentParams)=>{
        this.routerParams = componentParams;
        this.searchConditionForm = new WeaForm();
        this.tableStore = new TableStore();
    }

    @action
    componentDidMount=()=>{
        this.loading = true;
        let requestParams = {};
        requestParams[onlySessionKey] = false;
        this.getTableData(requestParams,this.componentInitCallBack);
    }

    @action
    componentWillUnmount=()=>{
        this.loading = false;
        this.viewTop = true;
        this.titleName = "";
        this.titleIcon = "";
        this.rightMenu = [];
        this.showSearchAd = false;
        this.searchKey = "";
        this.tableStore = {};
        this.routerParams = {};
        this.searchConditionForm = {};
        this.searchConditionComponents = {};
    }

    @action
    doSearch=(params = {})=>{
        this.loading = true;
        this.setShowSearchAd(false);
        let initParams = {};
        initParams[onlySessionKey] = true;
        initParams[this.searchInputName] = this.searchKey;
        const searchParamsAd = this.searchConditionForm.getFormParams();
        let requestParams = {...initParams,...searchParamsAd,...params};
        for(let item in requestParams){
            if(requestParams[item] == ""){
                delete requestParams[item]
            }
        }
        this.getTableData(requestParams,this.doSearchCallBack);
    }

    @action
    componentInitCallBack=(data = {})=>{
        const content = data.datas.content;
        this.titleName = content.titleName;
        this.titleIcon = content.titleIcon;
        this.rightMenu = content.rightMenu;
        this.searchConditionComponents = content.searchConditionComponents;
        // 初始化高级搜索
        this.searchConditionForm.initFormFields(content.searchConditionComponents);
        // 初始化表格数据
        this.tableStore.getDatas(content.sessionKey, 1);
        this.loading = false;
    }

    @action
    doSearchCallBack=(data = {})=>{
        const content = data.datas.content;
        this.tableStore.getDatas(content.sessionKey, 1);
        this.loading = false;
    }

    getRightMenus() {
        const { rightMenu, tableStore } = this;
        let btnArr = [];
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            btnArr.push({
                key: m.type,
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                onClick: key => {
                    if (key == Bctt.BTN_EXPORT) {
                        this.excelExport(this.excelExportCallBack);
                    } else if (key == Bctt.BTN_SEARCH) {
                        this.doSearch();
                    } else if (key == Bctt.BTN_COLUMN) {
                        tableStore.setColSetVisible(true);
                        tableStore.tableColSet(true);
                    } else{
                        this.rightMenuClick(key);
                    }
                }
            })
        });
        return btnArr;
    }

    getTopButtons() {
        const { rightMenu, tableStore } = this;
        let btnArr = [];
        rightMenu && rightMenu.length > 0 && rightMenu.map(m => {
            m.isTop == "1" && btnArr.length < 4 && btnArr.push(
                <Button type="primary"
                    disabled={m.isControl == '1' && toJS(tableStore.selectedRowKeys).length == 0}
                    onClick={() => {
                        let fn = m.menuFun.indexOf("this") >= 0 ? `${m.menuFun.split("this")[0]})` : m.menuFun;
                        if (m.type == Bctt.BTN_EXPORT) {
                            this.excelExport(this.excelExportCallBack);
                        }else{
                            this.rightMenuClick(m.type);
                        }
                    }}>
                    {m.menuName}
                </Button>
            );
        });
        let btnArrCustom = this.getTopButtonsCustom();
        return btnArrCustom.concat(btnArr);
    }

    getTopButtonsCustom() {
        let btnArr = [];
        return btnArr;
    }

    excelExportCallBack(data) {
        window.location = Bctt.ADDR_EXCEL_EXPORT;
    }
    
    rightMenuClick(key){

    }

    @action
    setShowSearchAd=(bool = false)=>{
        this.showSearchAd = bool;
    }

    @action
    setSearchKey=(v = "")=>{
        this.searchKey = v;
        try {
            let searchKeyField = {};
            searchKeyField[this.searchInputName] = {value : v};
            this.searchConditionForm.updateFields(searchKeyField,false);  
        } catch (error) {
          // 高级搜索中没有searchKey的情况
        }
    }

    @action
    searchConditionFieldChange=(fieldObj)=>{
        if(fieldObj[this.searchInputName]){
            this.searchKey = fieldObj[this.searchInputName].value;
        }
    }

    clearAdFields=()=>{
        this.searchConditionForm.reset();
        this.searchKey = "";
    }

    excelExport(excelExportCallBack){

    }
}

export default BaseListStore;